package com.sanchunhui.psc.ui.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.Toast
import com.sanchunhui.psc.R
import com.sanchunhui.psc.common.BaseApplication
import com.sanchunhui.psc.data.protocol.ResponseInfo
import com.sanchunhui.psc.injection.component.DaggerProjectTeamComponent
import com.sanchunhui.psc.injection.module.ProjectTeamModule
import com.sanchunhui.psc.presenter.AddTeamPresenter
import com.sanchunhui.psc.presenter.view.AddTeamView
import com.sanchunhui.ui.activity.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_team_set_info.ib_back
import kotlinx.android.synthetic.main.activity_team_set_info.tv_title
import kotlinx.android.synthetic.main.activity_add_team.*
import kotlinx.android.synthetic.main.activity_team_members.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * @Author zyx
 * @Date 2021/7/7-15:18
 * @Dscribe
 */
class AddTeamActivity : BaseMvpActivity<AddTeamPresenter>(),AddTeamView, View.OnClickListener{

    private lateinit var teamLeaderName : String
    private lateinit var leaderMemberId : String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_team)
        initView()
    }

    fun initView() {
        tv_title.text = "添加小组"
        ib_back.setOnClickListener(this)
        ib_add.setOnClickListener(this)
        doneTv.setOnClickListener(this)
    }

    fun checkTeamInfo(): Boolean {
        val teamName = et_team_name.getText().toString().trim()
        if (TextUtils.isEmpty(teamName)) {
            Toast.makeText(this, "请填写小组名称", Toast.LENGTH_SHORT).show()
            return false
        }
        val teamLeader = et_team_leader.getText().toString().trim()
        if (TextUtils.isEmpty(teamLeader)) {
            Toast.makeText(this, "请添加组长", Toast.LENGTH_SHORT).show()
            return false
        }
        return true
    }

    override fun onClick(v: View?) {
        when(v?.id) {
            R.id.ib_back -> finish()
            R.id.doneTv -> {
                val isOk = checkTeamInfo()
                if (isOk) {
                    inserTeam()
                }
            }
            R.id.ib_add  -> {
                val intent : Intent = Intent(this, SearchMemberActivity::class.java)
                intent.putExtra("intentStr", "1")
                startActivityForResult(intent, 1)
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            1 -> {
                teamLeaderName = data!!.getStringExtra("teamLeaderName")
                leaderMemberId = data.getStringExtra("leaderMemberId")
//                et_team_leader.text =  Editable.Factory.getInstance().newEditable(teamLeaderName)
                et_team_leader.text =  teamLeaderName

            }

        }

    }

    fun inserTeam() {
        val user = BaseApplication.mUser
        var teamName = et_team_name.text.toString().trim()
        mPresenter.addTeam(user.token.toString(), teamName, leaderMemberId.toInt())
    }

    /*
       Dagger注册
    */
    override fun injectComponent() {
        DaggerProjectTeamComponent.builder().activityComponent(mActivityComponent).projectTeamModule(ProjectTeamModule()).build().inject(this)
        mPresenter.mView = this
    }

    override fun onGetAddTeamResult(result: ResponseInfo) {
        if (result.code == 0) {
            val toast = Toast.makeText(this, "添加小组成功", Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.show()
            finish()
        } else if (result.code == 401) {
            startActivity<LoginActivity>()
        } else {
            val toast = Toast.makeText(this, result.message, Toast.LENGTH_SHORT)
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.show()
        }

    }

}